/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.poker;

import log.Debug;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

/**
 *
 * @author daz
 */
public class BettingActionHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        PokerRoomExtension pokerRoomExtension = (PokerRoomExtension) getParentExtension();
        PokerGame pokerGame = pokerRoomExtension.getPokerGame();
        Response response = pokerRoomExtension.getResponse();
        //info receive
        int bettingChoice = params.getInt(PokerFlag.BETTING_CHOICE);
        long bettingMoney = params.getInt(PokerFlag.BETTING_MONEY);
        int bettingPlayer = pokerGame.getBettingTurn();
        Debug.d(user.getName() + " betting choice:" + bettingChoice + ",betting money :" + bettingMoney + ",betting player :" + bettingPlayer);
        PokerPlayer pokerPlayer = pokerGame.getListPlayers()[bettingPlayer];
        pokerPlayer.setBettingChoice(bettingChoice);
        pokerGame.betAction();
        switch (bettingChoice) {
            case PokerGame.FOLD:
                pokerGame.checkFinishWhenFold(bettingPlayer);
                bettingChoice = PokerGame.FOLD;
                bettingMoney = 0;
                break;
            case PokerGame.CHECK:
                bettingChoice = PokerGame.CHECK;
                bettingMoney = 0;
                break;

            case PokerGame.CALL:
                long moneyMustBet = pokerGame.getMoneyMustBet();
                bettingMoney = moneyMustBet - pokerPlayer.getBet();
                bettingChoice = PokerGame.CALL;
                if (bettingMoney > pokerPlayer.getMoney()) {
                    bettingMoney = pokerPlayer.getMoney();
                }
                Debug.d("contribute " + pokerPlayer.getName() + ":" + bettingMoney);
                pokerGame.contributePot(bettingMoney, bettingPlayer);
                pokerPlayer.betMore(bettingMoney);
                Debug.d(pokerPlayer.getName() + " money :" + pokerPlayer.getMoney());
                if (pokerPlayer.getMoney() <= 0) {
                    pokerGame.checkFinishWhenAllBet(bettingPlayer);
                }
                break;

            case PokerGame.RAISE:
                moneyMustBet = pokerGame.getMoneyMustBet();
                if (moneyMustBet == 0) {
                    bettingChoice = PokerGame.BET;
                }
                bettingMoney = (bettingMoney > pokerPlayer.getMoney() ? pokerPlayer.getMoney() : bettingMoney);
                pokerGame.setMustBet(bettingMoney + pokerPlayer.getBet());
                pokerPlayer.betMore(bettingMoney);
                Debug.d("contribute " + pokerPlayer.getName() + ":" + bettingMoney);
                pokerGame.contributePot(bettingMoney, bettingPlayer);
                Debug.d(pokerPlayer.getName() + " money :" + pokerPlayer.getMoney());
                if (pokerPlayer.getMoney() <= 0) {
                    pokerGame.checkFinishWhenAllBet(bettingPlayer);
                }
                break;
        }

        response.sendBettingAction(bettingChoice, bettingMoney, bettingPlayer, pokerPlayer.getMoney(), pokerRoomExtension.getParentRoom().getUserList());
        logBettingInfo(bettingChoice, bettingMoney, pokerPlayer.getName());
        if (bettingChoice >= 2) {
            pokerRoomExtension.getResponse().sendMessageChat(user.getName() + PokerConversion.getBetActionName(bettingChoice) + " " + bettingMoney, 0);
        } else {
            pokerRoomExtension.getResponse().sendMessageChat(user.getName() + PokerConversion.getBetActionName(bettingChoice), 0);
        }
        pokerGame.nextTurn();


    }

    private void logBettingInfo(int bettingChoice, long bettingMoney, String playerName) {
        Debug.i(playerName + "-" + PokerConversion.getBetActionName(bettingChoice) + ",B=" + bettingMoney);
    }
}
